package com.zhongyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhongyu.mapper.*;
import com.zhongyu.pojo.CangkuKucun;
import com.zhongyu.pojo.Goods;
import com.zhongyu.pojo.KucunBiandong;
import com.zhongyu.pojo.KucunPandian;
import com.zhongyu.service.KuCunPdService;
import com.zhongyu.vo.KuCunBdVo;
import com.zhongyu.vo.KuCunPdVo;
import com.zhongyu.vo.PageResult;
import com.zhongyu.vo.ResultVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service("KuCunPdService")
public class KuCunPdServicelmpl  extends ServiceImpl<KuCunPdMapper, KucunPandian> implements KuCunPdService {
  @Resource
  private  KuCunPdMapper kuCunPdMapper;
    @Resource
    private GoodsMapper goodsMapper;
    @Resource
    private CangkuKuCunMapper kuCunMapper;
    @Resource
    private KuCunBdMapper kuCunBdMapper;
    @Override
    public PageResult searchAll(Integer pageNo, Integer pageSize, KuCunPdVo kuCunPdVo) {
        LambdaQueryWrapper<KucunPandian> wrapper= Wrappers.<KucunPandian>lambdaQuery();
        wrapper.eq(kuCunPdVo.getUserId()!=null,KucunPandian::getUserIds,kuCunPdVo.getUserId())
                .ge(kuCunPdVo.getStartDate()!=null,KucunPandian::getOpertime,kuCunPdVo.getStartDate())
                .le(kuCunPdVo.getEndDate()!=null,KucunPandian::getOpertime,kuCunPdVo.getEndDate())
                .eq(kuCunPdVo.getCangkuId()!=null,KucunPandian::getCangkuId,kuCunPdVo.getCangkuId())
                .eq(kuCunPdVo.getGoodsId()!=null,KucunPandian::getGoodsId,kuCunPdVo.getGoodsId())
                .eq(kuCunPdVo.getGoodsCode()!=null,KucunPandian::getGoodsCode,kuCunPdVo.getGoodsCode())
                .eq(kuCunPdVo.getResult()!=null,KucunPandian::getResult,kuCunPdVo.getResult());
        Page page=new Page(pageNo,pageSize);
        List<KucunPandian> list=baseMapper.findAll(page,wrapper);
        return new PageResult(list,page.getTotal());
    }

    @Override
    public List<KucunPandian> searchAll() {
        return null;
    }

    @Override
    public List<KucunPandian> searchById(Integer cangkuId, Integer goodsId) {
        return baseMapper.findById(cangkuId,goodsId);
    }

    @Override
    public ResultVo kuCunPanDian(CangkuKucun cangkuKuCun, Integer userId, Integer searchNum,String beizhu) {
                int num =-1;
                 Goods goods =goodsMapper.findById(cangkuKuCun.getGoodsId());
                if(cangkuKuCun.getShuliang().intValue()<searchNum.intValue()){
                    num=searchNum.intValue()-cangkuKuCun.getShuliang().intValue();
                    KucunPandian kucunPandian=new KucunPandian();
                    kucunPandian.setUserIds(userId);
                    kucunPandian.setOpertime(LocalDateTime.now());
                    kucunPandian.setCangkuId(cangkuKuCun.getCangkuId());
                    kucunPandian.setGoodsId(cangkuKuCun.getGoodsId());
                    kucunPandian.setGoodsCode(goods.getCode());
                    kucunPandian.setBeizhu(beizhu);
                    kucunPandian.setResult("2");
                    kucunPandian.setNum(cangkuKuCun.getShuliang());
                    kucunPandian.setSearchNum(searchNum);
                    kuCunPdMapper.insert(kucunPandian);
                     //更新库存数量
                    cangkuKuCun.setShuliang(searchNum);
                    kuCunMapper.updateById(cangkuKuCun);
                    //添加库存变动信息
                    KucunBiandong kucunBiandong = new KucunBiandong();
                    kucunBiandong.setUserid(userId);
                    kucunBiandong.setGoodsId(cangkuKuCun.getGoodsId());
                    kucunBiandong.setNum(num);
                    kucunBiandong.setOpertime(LocalDateTime.now());
                    kucunBiandong.setCangkuId(cangkuKuCun.getCangkuId());
                    kucunBiandong.setType("1");
                    kuCunBdMapper.insert(kucunBiandong);
                    return ResultVo.success("库存盘点成功");
                }else if(cangkuKuCun.getShuliang().intValue()>searchNum.intValue()){
                    num=cangkuKuCun.getShuliang().intValue()-searchNum.intValue();
                    KucunPandian kucunPandian=new KucunPandian();
                    kucunPandian.setUserIds(userId);
                    kucunPandian.setOpertime(LocalDateTime.now());
                    kucunPandian.setCangkuId(cangkuKuCun.getCangkuId());
                    kucunPandian.setGoodsId(cangkuKuCun.getGoodsId());
                    kucunPandian.setGoodsCode(goods.getCode());
                    kucunPandian.setBeizhu(beizhu);
                    kucunPandian.setResult("2");
                    kucunPandian.setNum(cangkuKuCun.getShuliang());
                    kucunPandian.setSearchNum(searchNum);
                    kuCunPdMapper.insert(kucunPandian);
                   //更新库存数量
                    cangkuKuCun.setShuliang(searchNum);
                    kuCunMapper.updateById(cangkuKuCun);
                    //添加库存变动信息
                    KucunBiandong kucunBiandong = new KucunBiandong();
                    kucunBiandong.setUserid(userId);
                    kucunBiandong.setGoodsId(cangkuKuCun.getGoodsId());
                    kucunBiandong.setNum(num);
                    kucunBiandong.setOpertime(LocalDateTime.now());
                    kucunBiandong.setCangkuId(cangkuKuCun.getCangkuId());
                    kucunBiandong.setType("2");
                    kuCunBdMapper.insert(kucunBiandong);
                    return ResultVo.success("库存盘点成功");
                }
                    KucunPandian kucunPandian=new KucunPandian();
                    kucunPandian.setUserIds(userId);
                    kucunPandian.setOpertime(LocalDateTime.now());
                    kucunPandian.setCangkuId(cangkuKuCun.getCangkuId());
                    kucunPandian.setGoodsId(cangkuKuCun.getGoodsId());
                    kucunPandian.setGoodsCode(goods.getCode());
                    kucunPandian.setResult("1");
                    kucunPandian.setNum(cangkuKuCun.getShuliang());
                    kucunPandian.setSearchNum(searchNum);
                    kuCunPdMapper.insert(kucunPandian);
                    return ResultVo.success("库存盘点成功");
    }
}
